// Decompiled by Jad v1.5.8g. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.kpdus.com/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   SearchCanvas.java

package canvas;

import java.io.InputStream;
import java.util.Vector;
import javax.microedition.lcdui.*;

// Referenced classes of package mquran:
//            QuranCanvas

public class SearchCanvas extends Canvas
    implements Runnable
{

    public SearchCanvas(String string)
    {
        query = string.getBytes();
        surah = 1;
        founded = 0;
    }

    protected void paint(Graphics g)
    {
        g.setColor(0xffffff);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(0);
        if(surah <= QuranCanvas.SURAH.length)
            g.drawString("Searching.... [" + QuranCanvas.SURAH[surah - 1] + "]", 10, getHeight() / 2, 20);
        else
            g.drawString("Finish", 10, getHeight() / 2, 20);
        g.drawString("Found:" + founded, 10, getHeight() / 2 + 10, 20);
    }

    public void run()
    {
        int sub_surah = 0;
        int ayah = 0;
        Vector vector = new Vector();
        while(surah <= 114) 
        {
            if(sub_surah == 0)
            {
                Class clazz = getClass();
                InputStream is = clazz.getResourceAsStream("/bin/" + surah + ".bin");
                QuranCanvas.buffer_len = QuranCanvas.readResource(QuranCanvas.buffer, is);
            } else
            {
                Class clazz = getClass();
                InputStream is = clazz.getResourceAsStream("/bin/" + surah + "-" + sub_surah + ".bin");
                QuranCanvas.buffer_len = QuranCanvas.readResource(QuranCanvas.buffer, is);
            }
            for(int i = 0; i < QuranCanvas.buffer_len;)
                if(QuranCanvas.buffer[i] == 60 && QuranCanvas.buffer[i + 1] == 97)
                {
                    i++;
                    ayah++;
                    int n = 0;
                    for(; i < QuranCanvas.buffer_len && QuranCanvas.buffer[i] != 62; i++);
                    for(i++; i < QuranCanvas.buffer_len && QuranCanvas.buffer[i] != 10; i++)
                    {
                        if(query[n] == QuranCanvas.buffer[i])
                        {
                            if(++n != query.length)
                                continue;
                            founded++;
                            n = 0;
                            break;
                        }
                        if(n <= 0)
                            continue;
                        n = 0;
                        if(query[n] == QuranCanvas.buffer[i])
                            n++;
                    }

                    for(; i < QuranCanvas.buffer_len && QuranCanvas.buffer[i] != 10; i++);
                    i++;
                } else
                {
                    i++;
                }

            if(++sub_surah < QuranCanvas.PART_SURAH[surah - 1])
            {
                sub_surah++;
            } else
            {
                sub_surah = 0;
                surah++;
                ayah = 0;
            }
            repaint();
        }
    }

    byte query[];
    int surah;
    int founded;
}
